package com.example.netlib.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.netlib.ActionUrl;
import com.example.netlib.R;
import com.example.netlib.netwrapper.LocalHttpClientHolder;
import com.example.netlib.netwrapper.NetHttpClientHolder;
import com.example.netlib.util.tip.SystemDialogFactory;
import com.szlanyou.thirdsdk.retrofit.client.RequestType;
import com.szlanyou.thirdsdk.retrofit.file.download.ClickViewRelatedHttpDownloadProgressResultHandler;

import java.io.File;
import java.util.UUID;

/**
 * 文件下载测试
 */
public class FileDownloadActivity extends BaseActivity {
    private static final String TAG = "FileDownloadActivity";
    private String REQUEST_CODE = "100";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_download);
    }

    /**
     * 文件下载测试
     * @param view
     */
    public void fileDownloadTest(View view) {
        ProgressDialog progressDialog = SystemDialogFactory.getProgressDialog(this,"下载中...");
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(true);
        int lastPointIndex = LocalHttpClientHolder.getInstance().getActionUrl().DOWNLOAD.lastIndexOf(".");
        String type = LocalHttpClientHolder.getInstance().getActionUrl().DOWNLOAD.substring(lastPointIndex);
        File outputFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + UUID.randomUUID() + type);
        ClickViewRelatedHttpDownloadProgressResultHandler clickViewRelatedHttpDownloadProgressResultHandler = new ClickViewRelatedHttpDownloadProgressResultHandler(view) {
            @Override
            public void onStart(String requestPageFlag,String requestCode) {
                Log.d(TAG, "onStart: ");
                super.onStart(requestPageFlag,requestCode);
                progressDialog.show();
            }

            @Override
            public void onSuccess(String requestPageFlag,String requestCode, File file) {
                Log.d(TAG, "下载成功 requestCode = " + requestCode + ",保存路径 " + file.getAbsolutePath());
                Toast.makeText(FileDownloadActivity.this, "下载成功 >> " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(String requestPageFlag,String requestCode, Throwable e) {
                Log.e(TAG, "onError: requestCode = " + requestCode);
                e.printStackTrace();
                Toast.makeText(FileDownloadActivity.this, "下载失败 " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(String requestPageFlag,String requestCode, String responseCode, String msg) {
                Log.e(TAG, "onFailure:requestCode = " + requestCode + ",responseCode = " + responseCode + ",msg = " + msg);
            }

            @Override
            public void onProgress(long currentSize, long totalSize) {
                int progress = (int) (currentSize * 100.0 / totalSize);
                Log.d(TAG, "下载中 " + progress + "%");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.setProgress(progress);
                    }
                });
            }

            @Override
            public void onComplete(boolean causeByError) {
                Log.d(TAG, "onComplete: ");
                super.onComplete(causeByError);
                progressDialog.dismiss();
                SystemDialogFactory.releaseProgressDialog();
            }
        };

        NetHttpClientHolder.getInstance().getHttpClient().newRequestBuilder().type(RequestType.FILE_DOWNLOAD)
                .url(NetHttpClientHolder.getInstance().getActionUrl().DOWNLOAD)
                .autoShowLoading(false)
                .addHeader("privateHeaderKey1","privateHeaderValue1")
                .addHeader("privateHeaderKey2",999999)
                .requestCode(REQUEST_CODE)
                .httpResultHandler(clickViewRelatedHttpDownloadProgressResultHandler)
                .setDownloadFileOutputDirOrFileName(outputFile)
                .request();
    }

    public void cancelDownloadTest(View view) {
        NetHttpClientHolder.getInstance().getHttpClient().cancel(REQUEST_CODE);
    }

    @Override
    protected void onDestroy() {
        NetHttpClientHolder.getInstance().getHttpClient().cancel(REQUEST_CODE);
        super.onDestroy();
    }
}